package com.woniu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.AccountClient;
import com.woniu.dto.AccountDto;
import com.woniu.exception.RebornException;
import com.woniu.mapper.FlowMapper;
import com.woniu.model.Flow;
import com.woniu.model.Result;
import com.woniu.service.FlowService;
import com.woniu.service.param.AddFlowParam;
import com.woniu.service.param.ModifyFlowParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@Service
@Transactional
public class FlowServiceImpl extends ServiceImpl<FlowMapper, Flow> implements FlowService {
    @Resource
    private FlowMapper flowMapper;
    @Resource
    private AccountClient accountClient;

    /**
     * 添加流程
     * @param param 添加param
     */
    @Override
    public void add(AddFlowParam param) {
        Integer createUser = param.getCreateUser();
        Result<AccountDto> byId = accountClient.getById(createUser);
        if (byId.getCode()!=200){
            throw new RebornException(byId.getMessage(),byId.getCode());
        }
        Flow flow = BeanUtil.toBean(param, Flow.class);
        flow.setCreateTime(LocalDateTime.now());
        flowMapper.insert(flow);
    }


    /**
     * 修改流程
     * @param param 修改流程param
     */
    @Override
    public void modify(ModifyFlowParam param) {
        Integer createUser = param.getUpdateUser();
        Result<AccountDto> byId = accountClient.getById(createUser);
        if (byId.getCode()!=200){
            throw new RebornException(byId.getMessage(),byId.getCode());
        }
        Flow flow = flowMapper.selectById(param.getId());
        if (flow==null) {
            throw new RebornException("不存在的流程表",60001);
        }
        flow.setUpdateTime(LocalDateTime.now());

    }
}
